<?php
class PersonasController extends AppController{
	var $name = 'Personas';
	var $components = array('Paginator');
	var $helpers = array('Html', 'Form');
	
	function index(){
		//$this->set('personas', $this->Persona->find('all'));	
		$this->paginate = array(
				'order' => 'id',
				'conditions' => array('Deleted' => 0),
				'prefix' => 'Persona',
				'limit' => 10
		);
		
		$this->set('personas', $this->Paginator->paginate($this->Persona));
	}
	
	function add(){
		
		if(!empty($this->data)){
			
			$persona = array();
			$this->Persona->create();
			$this->Persona->id = null;
			$persona['Persona']['nombre'] = $this->data['Persona']['nombre'];
			$persona['Persona']['apellido_paterno'] = $this->data['Persona']['apellido_paterno'];
			$persona['Persona']['apellido_materno'] = $this->data['Persona']['apellido_materno'];
			$persona['Persona']['rut'] = $this->data['Persona']['rut'];
			$persona['Persona']['telefono'] = $this->data['Persona']['telefono'];
			$persona['Persona']['celular'] = $this->data['Persona']['celular'];
			$persona['Persona']['email'] = $this->data['Persona']['email'];
			$persona['Persona']['direccion'] = $this->data['Persona']['direccion'];
			$persona['Persona']['empresa'] = $this->data['Persona']['empresa'];
			$persona['Persona']['cargo'] = $this->data['Persona']['cargo'];
			$persona['Persona']['foto'] = $this->data['Persona']['foto']['name'];
			$persona['Persona']['id_pais'] = $this->data['Persona']['pais'];
			$persona['Persona']['id_region'] = $this->data['Persona']['region'];
			$persona['Persona']['id_comuna'] = $this->data['Persona']['comuna'];
			$persona['Persona']['fecha_nacimiento'] = $this->data['Persona']['FechaNacimiento'];
			$persona['Persona']['username'] = $this->data['Persona']['username'];
			$persona['Persona']['password'] = $this->data['Persona']['password'];
			
			if($this->Persona->save($persona)){
				$this->Session->setFlash('La persona ha sido ingresada correctamemte.');
				$this->redirect(array('action' => 'index'), null, true);			
			} 
			else{
				$this->Session->setFlash('Persona no ingresada. Pruebe de nuevo.');
			}
		}
	}

	function edit($id = null){
		
		if(!$id){
			$this->Session->setFlas('Persona Invalida');
			$this->redirect(array('action'=>'index'), null, true);
		}
		
		if(empty($this->data)){
			$this->data = $this->Persona->find(array('id' => $id));	
		}
		else{
			if($this->Persona->save($this->data)){
				$this->Session->setFlash('La persona ha sido editada');
				$this->redirect(array('action' => 'index'), null, true);
			}
			else{
				$this->Session->setFlash('La persona no ha podido ser editada, intentelo de nuevo');
			}
		}
	}

	function delete($id = null){
		if(!$id){
			$this->Session->setFlash('id invalido para el Persona');
			$this->redirect(array('action' => 'index'), null, true);
		}
		else{
			$persona = array();
			$this->Persona->id = null;
			$persona['Persona']['id'] = $id;
			$persona['Persona']['deleted'] = 1;
			if($this->Persona->save($persona)){
				$this->Session->setFlash('Persona #' . $id . ' borrado');	
				$this->redirect(array('action' => 'index'), null, true);
			}
		}
	}
	
	function reporte(){
		$personas = $this->Persona->find('all', array('recursive' => 1, 'conditions' => array('Persona.deleted' => 0)));	
		
		header('Content-type: application/vnd.ms-excel');
			header("Content-Disposition: attachment; filename=\"Reporte_Personas.xls\"");
			header("Pragma: no-cache");
			header("Expires: 0");
echo <<<html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
  <title>eClass</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
html;

			echo '<table border="1">'
			. '<tr>'
			. '<th>Id</th>'
			. '<th>Nombre</th>'
			. '<th>Apellido Paterno</th>'
			. '<th>Apellido Materno</th>'
			. '<th>Rut</th>'
			. '<th>Email</th>'
			. '<th>Telefono</th>'
			. '<th>Celular</th>'
			. '<th>Direccion</th>'
			. '<th>Localidad</th>'
			. '<th>Region</th>'
			. '<th>Pais</th>'
			. '<th>Empresa</th>'
			. '<th>Cargo</th>'
			. '</tr>'
			;

			if (empty($personas)) {
			echo '
			<tr>
				<td colspan="14" class="center">' . __('No se encontraron registros.', true) . '</td>
			</tr>';
			}
			else {
				foreach ($personas as $persona) {
					echo ' 
						<tr>
							<td class="center">' . $persona['Persona']['id'] . '</td>
							<td class="center">' . $persona['Persona']['nombre'] . '</td>
							<td class="center">' . $persona['Persona']['apellido_paterno'] . '</td>
							<td class="center">' . $persona['Persona']['apellido_materno'] . '</td>
							<td class="center">' . $persona['Persona']['rut'] . '</td>
							<td class="center">' . $persona['Persona']['email'] . '</td>
							<td class="center">' . $persona['Persona']['telefono'] . '</td>
							<td class="center">' . $persona['Persona']['celular'] . '</td>
							<td class="center">' . $persona['Persona']['direccion'] . '</td>
							<td class="center">' . $persona['Persona']['localidades_id'] . '</td>
							<td class="center">' . $persona['Persona']['regiones_id'] . '</td>
							<td class="center">' . $persona['Persona']['paises_id'] . '</td>
							<td class="center">' . $persona['Persona']['empresa'] . '</td>
							<td class="center">' . $persona['Persona']['cargo'] . '</td>
					</tr>';
				}
			}
			echo '</table>';
echo <<<html
</body>
</html>
html;

	}
}
?>